package com.control.qct.businesssteward.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.AsInformationActivity;
import com.control.qct.businesssteward.activity.SettingActivity;
import com.control.qct.businesssteward.activity.StoreSelectActivity;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.GlideUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

import static android.app.Activity.RESULT_OK;

@SuppressLint("ValidFragment")
public class MySelfFragment extends BaseFragment {
    @BindView(R.id.civ_head_picture)
    CircleImageView mCivHeadPicture;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.tv_store_name)
    TextView mTvStoreName;
    //    @BindView(R.id.tv_employee_information)
//    TextView mTvEmployeeInformation;
    //    @BindView(R.id.tv_safety_management)
//    TextView mTvSafetyManagement;
    @BindView(R.id.tv_setting)
    TextView mTvSetting;//设置
    @BindView(R.id.tv_as_information)
    TextView mTvAsInformation;//关于

    private String mUsername;
    private final static int PERSONAGE_CODE = 0x001001;
    private final static int STORE_SELECT_CODE = 0x001002;
    private Bundle mBundle;
    private String mStoreId = "";

    public MySelfFragment(String username) {
        mUsername = username;
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_myself, container, false);
    }

    @Override
    protected void initView(View view) {
        mTvName.setText(mUsername);
        StoreModel storeModel = (StoreModel) mSPUtils.getObject(mNetProtocol.getUserCode() + "_store");
        if (storeModel != null) {
            mTvStoreName.setText(storeModel.getTitle());
            mStoreId = storeModel.getStoreId();
        }
        GlideUtil.loadImage(activity, mSPUtils.getString("head_picture"), mCivHeadPicture);
    }


    @OnClick({R.id.rl_personage_information, R.id.rl_store_select, R.id.tv_setting, R.id.tv_as_information})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_personage_information://个人信息
//                startActivityForResult(PersonageInformationActivity.class, PERSONAGE_CODE);
                break;
            case R.id.rl_store_select://门店选择
                mBundle = new Bundle();
                mBundle.putString("store_id", mStoreId);
                startActivityForResult(StoreSelectActivity.class, STORE_SELECT_CODE, mBundle);
                break;
//            case R.id.tv_employee_information://员工信息
//                startActivity(EmployeeListActivity.class);
//                break;
//            case R.id.tv_safety_management://安全管理
//                startActivity(SafetyManagementActivity.class);
//                break;
            case R.id.tv_setting://设置
                startActivity(SettingActivity.class);
                break;
            case R.id.tv_as_information://关于
                startActivity(AsInformationActivity.class);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PERSONAGE_CODE:
                    if (data != null) {
                        String head_picture = data.getStringExtra("head_picture");
                        if (!TextUtils.isEmpty(head_picture)) {
                            GlideUtil.loadImage(activity, head_picture, mCivHeadPicture);
                        }
                        String name = data.getStringExtra("name");
                        if (!TextUtils.isEmpty(name)) {
                            mTvName.setText(name);
                        }
                    }
                    showShortToast("个人信息更新！");
                    break;
                case STORE_SELECT_CODE:
                    StoreModel storeModel = (StoreModel) data.getSerializableExtra("store");
                    mStoreId = storeModel.getStoreId();
                    mTvStoreName.setText(storeModel.getTitle());
                    break;
            }
        }
    }
}
